/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;


import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.Interaction;
import org.eclipse.uml2.uml.MessageEnd;
import org.eclipse.uml2.uml.NamedElement;

import com.cea.papyrus.diagram.sequence.sedi.editor.message.MessageHelper;

/**
 * Base class for commands that handle messages. For more detailed information about the
 * attributes, see subclasses.
 * 
 * @author frankd@ifi.uio.no
 */
public abstract class AbstractMessageCommand extends Command {

    protected MessageEndPosition sendPosition;
    protected MessageEndPosition receivePosition;

    public void setSendPosition(final MessageEndPosition sendPosition) {
        this.sendPosition = sendPosition;
    }

    public void setReceivePosition(final MessageEndPosition receivePosition) {
        this.receivePosition = receivePosition;
    }

    @Override
    public void dispose() {
        sendPosition = null;
        receivePosition = null;
    }

    protected MessageEnd createMessageEnd(final MessageEndPosition position, final boolean send) {
        MessageHelper helper = getMessageHelper(position);
        return helper.createMessageEnd(position, send);
    }

    protected void addMessageEnd(final MessageEndPosition position, final MessageEnd end,
            final boolean send) {
        MessageHelper helper = getMessageHelper(position);
        helper.addMessageEnd(position, end, send);
    }

    protected void sendMessage(final MessageEnd end, final NamedElement signature) {
        MessageHelper helper = getMessageHelper(end);
        helper.sendMessage(end, signature);
    }

    protected void receiveMessage(final MessageEnd end, final NamedElement signature) {
        MessageHelper helper = getMessageHelper(end);
        helper.receiveMessage(end, signature);
    }

    protected void removeMessage(final MessageEnd end) {
        MessageHelper helper = getMessageHelper(end);
        helper.removeMessage(end);
    }

    protected MessageEndPosition removeMessageEnd(final MessageEnd end) {
        MessageHelper helper = getMessageHelper(end);
        return helper.removeMessageEnd(end);
    }

    protected Interaction getInteraction(final MessageEndPosition position) {
        MessageHelper helper = getMessageHelper(position);
        return helper.getInteraction(position);
    }

    private MessageHelper getMessageHelper(final Object position) {
        MessageHelper helper = MessageHelper.select(position);
        assert helper != null;
        return helper;
    }
}
